<!--
  登录页专用文字上移样式，行高，宽度，密码框与通用样式不一致
  autoComplete="new-password"
-->
<template>
  <div class="gz-input">
    <div class="gz-text-up">
      <a-input 
        ref="inputRef"
        :type="inputPassword"
        :value="value"
        :required="isGoogle"
        :disabled="props.disabled"
        autoComplete="new-password"
        @change="onChange($event)"
      />
      <label @click="inputRef.focus()">{{ placeholder }}</label>
    </div>
    <div v-if="isPassword == 'password'" class="eyes" @click="changeEyes">
      <!-- 密码框的小眼睛 -->
      <eye-outlined v-if="!eyes" />
      <eye-invisible-outlined v-else />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {defineProps, ref} from 'vue'

// 定义input对象
const inputRef = ref()

// 定义父组件的传参和类型
const props = defineProps({
  value: { type: String, default: null },
  placeholder: { type: String, default: '' }, 
  isPassword: { type: String, default:'text' }, // 是否为密码框
  isGoogle: { type: String, default: 'required' }, // 谷歌验证码是否必填
  disabled: {type: Boolean, default: false }  // 是否禁用
})

// nextTick(() => {
//   // inputRef.value.focus()
//   if(!props.value) {
//       inputRef.value.blur()
//     }
//   // watch(() => props.value, () => {
    
//   // })
// })

// 密码的显示与隐藏
let eyes = ref(true) 
let inputPassword = ref(props.isPassword)
const changeEyes = () => {
  eyes.value = !eyes.value
  eyes.value ? inputPassword.value = 'password' : inputPassword.value = 'text'
}
const emit = defineEmits(['update:value','update:strength'])
const onChange =(e)=> {
  emit('update:value', e.target.value )

  // 密码框处理 如果是密码框的判断当前秘密的强度
  if(props.isPassword == 'password') {
    let strength = 0
    // 如果密码长度小于8大于0则密码强度strength是1
    if(e.target.value.length > 0 && e.target.value.length < 8) {
      strength = 1
    }else{
      strength = calculateStrength(e.target.value)
    }
    emit('update:strength', strength)
  }
}

// 计算强度
function calculateStrength(password) {
  // 定义字符类别的正则表达式
  const hasDigit = /\d/ // 检查是否包含数字
  const hasLetter = /[a-zA-Z]/ // 检查是否包含字母
  const hasSpecial = /[^\w\s]/ // 检查是否包含特殊字符（除了字母、数字和空格以外的所有字符）

  // 检查密码中是否包含每种字符类型
  const containsDigit = hasDigit.test(password)
  const containsLetter = hasLetter.test(password)
  const containsSpecial = hasSpecial.test(password)

  // 计算强度
  let strength = 0
  if (containsDigit) strength++
  if (containsLetter) strength++
  if (containsSpecial) strength++

  return strength
}
</script>

<style scoped lang="less">
.gz-input {
  position: relative;
  .eyes {
    position: absolute;
    right: 0;
    top: 0;
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 99;
  }
}
// 文字上移 效果
.gz-text-up {
  position: relative;
  
  input {
    outline: 0;
    transition: all .2s ease;
    height: 40px;
    max-width: 300px;
  }
  input::-webkit-input-placeholder {
    color: transparent;
    font-size: 13px;
  }
  .placeShow::-webkit-input-placeholder {
    color: #BFBFBF;
  }
  label {
    margin-left: 6px;
    position: absolute;
    left: 0;
    bottom: 13px;
    padding: 0px 5px;
    color: #BFBFBF;
    font-size: 13px;
    transition: all .2s ease;
    border-radius: 3px;
    line-height: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: initial;
  }

  input:focus + label,
  input:active + label,
  input:valid + label {
    font-weight: normal;
    font-size: 12px;
    letter-spacing: 0.05em;
    text-align: left;
    color:@primary-color;
    background: #fff; // 更换背景色
    transform: translateY(-22px);
  }

  input:not(:focus) + label {
    color: #B0AFB3;
    background: #fff;
  }
  
}

</style>
